#Copyright 2012 Elias Farhan
#
#This file is part of Squirrel.
#
#Squirrel is free software: you can redistribute it and/or modify it under the terms of the GNU General 
#Public License as published by the Free Software Foundation, either version 3 of the License, or (at your 
#option) any later version.
#
#Squirrel is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
#without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
#See the GNU General Public License for more details.
import lxml.etree as ET
import re
from box import Box
from gameobject import Character, GameObject
from level import Level
def XML2char(filename):
	tree = ET.parse(filename)
	root = tree.getroot()
	fname = root.get("filename")
	pos = tuple(int(v) for v in re.findall("[0-9]+", root.get("pos")))
	size = tuple(int(v) for v in re.findall("[0-9]+", root.get("size")))
	tmpCharacter = Character(pos, size, fname)
	stateindex = 0
	for state in root[0]:
		name = state.get("name")
		speed = float(state.get("speed"))
		frames = tuple(int(v) for v in re.findall("[0-9]+", state.get("frames")))
		index = int(state.get("index"))
		tmpCharacter.addState(name, speed, frames, index)
		for box in state[0]:
			tmpCharacter.addBox(Box(tuple(int(v) for v in re.findall("[0-9]+", box.get("origin"))), \
									tuple(int(v) for v in re.findall("[0-9]+", box.get("end")))), stateindex)
	return tmpCharacter
def XML2level(filename):
	tree = ET.parse(filename)
	root = tree.getroot()
	name = root.get("name")
	tmpLevel = Level(name)
	gameobjectindex = 0
	for gameobject in root[0]:
		filename = gameobject.get("filename")
		size = tuple(int(v) for v in re.findall("[0-9]+", gameobject.get("size")))
		pos = tuple(int(v) for v in re.findall("[0-9]+", gameobject.get("pos")))
		tmpLevel.addGameObject(GameObject(pos, size, filename))
		for box in gameobject[0]:
			tmpLevel.addBox(Box(tuple(int(v) for v in re.findall("[0-9]+", box.get("origin"))), \
									tuple(int(v) for v in re.findall("[0-9]+", box.get("end")))), gameobjectindex)
	return tmpLevel
#test for XML2Level
#level = XML2level("../../editor/file.xml")
#print level
